Worker
What if virtual network and worker_threads message passing mechanism combine together? You can work with worker_threads in almost the same way virtual network work. @cellularjs/worker
does not attempt to use thread for just a single task. Once net-worker is initialized, you can invoke any service that this net-worker exposed.
caution
Multithreading is NOT always better. When it come to performance, you should have some measurements. If your app don't have CPU-intense tasks, async is good enough.
#
1. Installationyarn add @cellularjs/worker
Features:
- Integrated with @cellularjs/net(you can work with @cellularjs/di too).
- Request-response communication from main thread to child thread.
- Thread pool with fixed number of worker.
- Multiple thread pools.
- You need more, please tell me?
note
To prevent communication deadlock, transferring message from child thread to other is not allowed.
Do you want to give it a try?
$ git clone https://github.com/cellularjs/http-sample.git$ cd http-sample$ yarn$ yarn dev:worker
Open this link for testing: http://localhost:3002/api/worker/test
#
2. Usage#
2.1. Create pool of net-workerExample 1: create a main network and a pool of 12 net-workers.
// src/$gateway/http/index.tsimport { isMainThread } from 'worker_threads';import { createNetWork, createPool, initNetWorker } from '@cellularjs/worker';import { appNetwork } from '$share/network/app.net';
!isMainThread && initNetWorker(appNetwork);
isMainThread && (async () => { // For simplicity, in this example, we use current file as worker script. // You can make use of CellularJS CLI to create separate worker source. await createPool({ script: __filename, minThread: 12 }); await createNetWork(appNetwork);
// ...})();
Example 2: create pool with custom name.
// src/$gateway/http/index.tsimport { isMainThread } from 'worker_threads';import { createNetWork, createPool, initNetWorker } from '@cellularjs/worker';import { appNetwork } from '$share/network/app.net';
!isMainThread && initNetWorker(appNetwork);
isMainThread && (async () => { await createPool({ name: 'halo', script: __filename, minThread: 12, });
await createNetWork(appNetwork);
// ...})();
#
2.2. Transfer IRQ to net-workerExample 1: transfer IRQ message to net-worker of default pool.
import { IRQ } from '@cellularjs/net';import { transfer } from '@cellularjs/worker';
(async () => { const cpuIntensiveTask = new IRQ( { to: 'Abc:Xyz' }, { data: '...' }, );
const irs = await transfer(cpuIntensiveTask);})();
Example 2: transfer IRQ message to net-worker of specific pool.
import { IRQ } from '@cellularjs/net';import { transfer } from '@cellularjs/worker';
(async () => { const cpuIntensiveTask = new IRQ( { to: 'Abc:Xyz' }, { data: '...' }, );
const irs = await transfer(cpuIntensiveTask, { pool: 'halo', });})();